-------------------------------------NTO PERIODICI

select * from (

select distinct 
--CODICE_CONTRATTO, STATO_CONTRATTO, MOVIMENTO, periodo,
'PNO' "CodFlusso",
PIvaUtente "PIvaUtente",
PIvaDistributore  "PIvaDistributore",
CodContrDisp "CodContrDisp",
a.Pod "Pod",
to_char(decode(movimento,'RASC',DataMisura-1,'RACC',DataMisura,DataMisura),'dd/mm/yyyy') "DataMisura",
Trattamento  "Trattamento",
Tensione "Tensione",
PotContrImp "PotContrImp",
PotDisp "PotDisp",
Ka "Ka",
Kr  "Kr",
Kp "Kp",
MatrAtt   "MatrAtt",
MatrRea "MatrRea",
MatrPot  "MatrPot",
to_char(DataInstMisAtt,'dd/mm/yyyy') "DataInstMisAtt",
to_char(DataInstMisRea,'dd/mm/yyyy')  "DataInstMisRea",
to_char(DataInstMisPot,'dd/mm/yyyy') "DataInstMisPot",
CifreAtt  "CifreAtt",
CifreRea  "CifreRea",
CifrePot  "CifrePot",
GruppoMis "GruppoMis",
Forfait "Forfait",
Raccolta "Raccolta",
TipoDato "TipoDato",
Validato  "Validato",
--EaM  "EaM",
--ErM "ErM",
--PotM "PotM",
--EaF1 "EaF1",
--EaF2 "EaF2",
--EaF3 "EaF3",
--ErF1 "ErF1",
--ErF2 "ErF2",
--ErF3  "ErF3",
--PotF1 "PotF1",
--PotF2 "PotF2",
--PotF3 "PotF3",
decode (a.trattamento,'M' ,EaM,null)  "EaM",
decode (a.trattamento,'M' ,ErM,null) "ErM",
decode (a.trattamento,'M' ,PotM,null)"PotM",
decode (a.trattamento,'F' ,EaF1,null) "EaF1",
decode (a.trattamento,'F' ,EaF2,null) "EaF2",
decode (a.trattamento,'F' ,EaF3,null) "EaF3",
decode (a.trattamento,'F' ,ErF1,null) "ErF1",
decode (a.trattamento,'F' ,ErF2,null) "ErF2",
decode (a.trattamento,'F' ,ErF3,null) "ErF3",
decode (a.trattamento,'F' ,PotF1,null) "PotF1",
decode (a.trattamento,'F' ,PotF2,null) "PotF2",
decode (a.trattamento,'F' ,PotF3,null) "PotF3",


DataInizioPeriodo "DataInizioPeriodo",
decode (a.trattamento,'M' ,EaM_1,null) "EaM_1"

from asm_delib_6_12_001  A


where 
--(select 1 from asm_delib_6_12_EXCEL b where a.pod=b.pod) is not null
   periodo ='M'
 and trattamento <> 'O'
--and stato_contratto <> 'W'
--and pod = 'IT018E01472101'
--order by CodContrDisp,a.pod
 and ((
nvl(EaMc,0) +
nvl(ErMc,0)+
nvl(PotMc,0)+
nvl(EaF1c,0) +
nvl(EaF2c,0) +
nvl(EaF3c,0)+
nvl(ErF1c,0) +
nvl(ErF2C,0) +
nvl(ErF3C ,0)+
nvl(PotF1c,0) +
nvl(PotF2 ,0)+
nvl(PotF3c,0) )>0 or movimento in ('L','IA','RACC','RASC'))
)
--where  "Pod" ='IT018E00061201'
order by "Pod","CodContrDisp",to_date("DataMisura",'dd/mm/yyyy')

